﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using NLayer;
using SharpAudio.Codec;

namespace BITUI.Audio
{
    class AudioData_Mp3 : IAudioData
    {
        public string[] ExtNames => new string[] { ".mp3" };
        public int Freq => _mp3Stream.SampleRate;

        public int Channel => _mp3Stream.Channels;//這個stream讀取器把一切都變成了2channel  Mp3Stream.ChannelCount;

        public float Time => (float)_mp3Stream.Duration.TotalSeconds;

        public float ReadTime
        {
            get
            {

                return (float)_mp3Stream.Time.TotalSeconds;

            }

        }
        public void Reset()
        {
            //换了一个库
            _mp3Stream.Position = 0;
            //mp3stream 有bug，播放完就不能loop了，先用一个不太聪明的方法解决，回头去找他源码
            //_mp3Stream.Dispose();
            //_mp3Stream = new MpegFile(_stream);

        }
        public void SeekTo(float _time)
        {
            _mp3Stream.Time = TimeSpan.FromSeconds(_time);
        }
        public void Dispose()
        {
            _mp3Stream.Dispose();
            //Mp3Data.Dispose();
        }
        //MP3SharpFix.Mp3Data Mp3Data;
        //MP3SharpFix.Mp3StreamDecoder Mp3Decoder;

        private System.IO.Stream _stream;
        private MpegFile _mp3Stream;
        public bool Load(string filename)
        {
            _stream = System.IO.File.OpenRead(filename);
            return Load(_stream);
        }

        public bool Load(Stream stream)
        {

            _mp3Stream = new MpegFile(stream);
            _stream = stream;
            return true;
        }

        public int ReadFloat(float[] data, int offset, int count)
        {
            return _mp3Stream.ReadSamples(data, offset, count);
            //return Mp3Decoder.ReadFloat(data, offset, count);

        }
    }

}
